<?php
class admin_listController extends adminController{

    //首页
    public function index() {
        $this->class_list=model('special_category')->special_list();
        $this->show();  
    }

    //公共列表筛选
    public function common_list_where()
    {
        //排序
        $sequence=intval($_POST['sequence']);
        switch ($sequence) {
            case '1':
                $order='B.updatetime DESC';
                break;
            case '2':
                $order='B.updatetime ASC';
                break;
            case '3':
                $order='B.aid DESC';
                break;
            case '4':
                $order='B.aid ASC';
                break;
            case '5':
                $order='B.inputtime DESC';
                break;
            case '6':
                $order='B.inputtime ASC';
                break;
            case '7':
                $order='B.views DESC';
                break;
            case '8':
                $order='B.views ASC';
                break;
        }
        if(!empty($order)){
        $order=$order.',';
        }

        //栏目ID
        $cid=intval($_POST['cid']);
        if(!empty($cid)){
            $where[]=' AND A.cid='.$cid;
        }

        $str ='';
        if(!empty($where)){
            foreach ($where as $value) {
                $str .= $value;
            }
        }
        return array(
            'order'=>$order,
            'where'=>$str
        );

    }

    //AJAX获取内容列表
    public function ajax_content_list()
    {
        //分页参数
        $listRows=20;
        $page = intval($_POST['page']);
        $limit_start = ($page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;

        //获取公共信息条件
        $where=$this->common_list_where();

        //列表信息
        $list=model('special_list')->special_list($limit,$where['where'],$where['order']);
        $count=model('special_list')->special_count($where['where']);

        //总页数
        $countpage=ceil($count/$listRows);

        if($list){
            foreach ($list as $vo) {
                $html.='
                <tr id="del_'.$vo['id'].'">
                    <td><input name="id[]" type="checkbox" id="id[]" value="'.$vo['id'].'" ></td>
                    <td>'.$vo['id'].'</td>
                    <td><span>';
                if($vo['title']){
                    $html.=$vo['title'];
                }else{
                    $html.=$vo['atitle'];
                }
                $html.='</span>';
                if(!empty($vo['recommend'])){
                    $str_list=api('recommend','content_list',$vo['recommend']);
                    if(!empty($str_list)){
                        foreach ($str_list as $value) {
                            $html.=' <font color=green>['.$value['name'].']</font>';
                        }
                    }
                }
                $html.='
                    </td>
                    <td>'.$vo['cname'].'</td>
                    <td>';
                $html.=date('Y-m-d H:i:s',$vo['updatetime']);
                $html.='</td>
                    <td>'.$vo['sequence'].'</td>
                    <td>
                    <a class="operating" href="'.url('admin_list/edit',array('id' => $vo['id'])).'" ><span class="icon">&#xf040;</span> 修改</a>
                    <a class="operating" href="javascript:void(0);" onClick="del(\''.url('admin_list/del').'\',\''.$vo['id'].'\',this)"><span class="icon">&#xf014;</span> 删除</a>
                    </td>
                </tr>
                ';
            }
            $status=1;
        }else{
            $html='<tr><td colspan="7"><center>暂无查找到内容<center></td></tr>';
            $status=0;
        }

        @header("Content-type:text/html");
        echo json_encode(array('status' => $status, 'info' => $html ,'countpage'=>$countpage));
    }

    //查找内容
    public function search() {
        $this->category_list=api('admin','category_list',0);
        $this->show(); 
    }

     //AJAX获取内容列表
    public function search_list()
    {
        //分页参数
        $listRows=20;
        $page = intval($_POST['page']);
        $limit_start = ($page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;

        //获取公共信息条件
        $where=api('admin','content_common_list_where');
        $where['where']='A.status=1 '.$where['where'];

        //列表信息
        $list=api('content','content_list',array('limit'=>$limit,'where'=>$where['where'],'order'=>$where['order']));
        $count=api('content','count',$where['where']);

        //总页数
        $countpage=ceil($count/$listRows);

        if($list){
            foreach ($list as $vo) {
                $html.='
                <tr>
                    <td>'.$vo['aid'].'</td>
                    <td><span>';
                $html.=$vo['title'];
                $html.='</span>
                    </td>
                    <td>'.$vo['cname'].'</td>
                    <td>';
                $html.=date('Y-m-d H:i:s',$vo['updatetime']);
                $html.='</td>
                    <td>
                    <a href="javascript:void(0);" onClick="select(\''.$vo['aid'].'\',\''.$vo['title'].'\')"  ><span class="icon">&#xf040;</span> 选择</a>
                    </td>
                </tr>
                ';
            }
            $status=1;
        }else{
            $html='<tr><td colspan="7"><center>暂无查找到内容<center></td></tr>';
            $status=0;
        }

        @header("Content-type:text/html");
        echo json_encode(array('status' => $status, 'info' => $html ,'countpage'=>$countpage));
    }

    public function common_info($id=null){
        if($id){
            $this->recommend_array=model('special_recommend')->relation_array($id);
        }
        $this->recommend_list=api('recommend','recommend_list');
    }

    public function common_save_data($data){
        if(is_array($data['recommend'])){
            $data['recommend']=implode(',',$data['recommend']);
        }else{
            $data['recommend']='';
        }
        return $data;
    }

    //添加
    public function add() {
        $this->action_name='添加';
        $this->action='add';
        $this->class_list=model('special_category')->special_list();
        $this->common_info();
        $this->show('admin_list/info'); 
    }

    public function add_save() {
        //录入模型处理
        if(empty($_POST['aid'])){
            $this->msg('未选择内容',0);
        }
        $data=$this->common_save_data($_POST);
        $id=model('special_list')->add($data);
        model('special_recommend')->content_save($data['recommend'],$id);
        $this->msg('专题内容添加成功！',1);
    }

    //修改
    public function edit() {
        $id=$_GET['id'];
        $this->alert_str($id,'int');
        $this->common_info($id);
        //获取内容信息
        $this->info=model('special_list')->info($id);
        $this->content_info=api('admin','content_info',$this->info['aid']);
        $this->action_name='编辑';
        $this->action='edit';
        $this->class_list=model('special_category')->special_list();
        $this->show('admin_list/info'); 
    }

    public function edit_save() {
        $id=$_POST['id'];
        $this->alert_str($id,'int',true);

        if(empty($_POST['aid'])){
            $this->msg('未选择内容',0);
        }
        //录入模型处理
        $data=$this->common_save_data($_POST);
        model('special_list')->edit($data);
        model('special_recommend')->special_save($data['recommend'],$id);
        $this->msg('专题内容修改成功! ',1);
    }

    //删除
    public function del() {
        $id=intval($_POST['id']);
        $this->alert_str($id,'int',true); 
        //录入模型处理
        model('special_list')->del($id);
        model('special_recommend')->special_del($id);
        $this->msg('专题内容删除成功！',1);
    }

    //获取最新标题
    public function uptitle(){
        $aid=intval($_POST['aid']);
        if(empty($aid)){
            $this->msg('请先选择内容！',0);
        }
        $info=api('admin','content_info',$aid);
        if(empty($info)){
            $this->msg('暂时无法获取到最新标题！');
        }
        $this->msg($info['title']);
    }

    //批量操作
    public function batch(){
        if(empty($_POST['status'])||empty($_POST['id'])){
            $this->msg('操作无效！',0);
        }
        $id_array=substr($_POST['id'],0,-1);
        $id_array=explode(',', $id_array);
        if(empty($id_array)){
            $this->msg('请先选择内容！',0);
        }
        switch ($_POST['status']) {
            case '1':
                //删除
                foreach ($id_array as $value) {
                    model('special_list')->del($value);
                }
                break;
            case '2':
                //转移栏目
                $cid=intval($_POST['cid']);
                if(empty($cid)){
                    $this->msg('请先选择目标栏目！',0);
                }
                foreach ($id_array as $value) {
                    $data=array();
                    $data['id']=$value;
                    $data['cid']=$cid;
                    model('special_list')->edit($data);
                }
                break;
        }
        $this->msg('操作执行完毕！',1);

    }


}